#include "ShowCoin.h"


ShowCoin::ShowCoin(LPD3DXSPRITE SpriteHandler)
{
	this->_Texture = new Texture();
	this->_listCoins = new list<Coin*>();
	this->_Vy = 400;
	this->_SpriteHandler = SpriteHandler;
}


ShowCoin::~ShowCoin(void)
{
}

void ShowCoin :: Add(D3DXVECTOR2 Position)
{
	Coin* coin = new Coin();
	coin->_CurFrame = 0;
	coin->_NumFrame = 10;
	coin->_Position = Position;
	coin->_Time = timeGetTime();
	coin->_TimeAnimation = coin->_Time;
	this->_listCoins->push_back(coin);
}

void ShowCoin :: Update(int Deltatime)
{
	list<Coin*> :: iterator i;
	i = this->_listCoins->begin();

	while(i != this->_listCoins->end())
	{
		_Coin = (*i);
		if(timeGetTime() > (*i)->_Time + 300)
		{
			i = this->_listCoins->erase(i);
		}
		else
		{
			_Coin->_Position.y -= _Vy * Deltatime / 1000;
			if(timeGetTime() >= (*i)->_TimeAnimation + 50)
			{
				_Coin->_TimeAnimation = timeGetTime(); 

				_Coin->_Rect.left = (_Coin->_CurFrame % 5)*50;
				_Coin->_Rect.right = _Coin->_Rect.left + 50;
				_Coin->_Rect.top = (_Coin->_CurFrame / 5)*50;
				_Coin->_Rect.bottom = _Coin->_Rect.top + 50;

				_Coin->_CurFrame++;
				if(_Coin->_CurFrame > 7)
				{
					_Coin->_CurFrame = 0;
				}
			}

			i++;
		}
	}
}

void ShowCoin :: Draw()
{
	list<Coin*> :: iterator i;
	i = this->_listCoins->begin();

	while(i != this->_listCoins->end())
	{
		this->_DrawPosition = (*i)->_Position;
		this->_SpriteHandler->Draw(_Texture->GetImage(), &(*i)->_Rect, NULL, new D3DXVECTOR3(_DrawPosition.x, _DrawPosition.y, 0), D3DCOLOR_XRGB(255,255,255));
		i++;
	}
}